package com.anolesoft.twelvePlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.twelvePlan.dto.MesEqualCatalogDTO;
import com.anolesoft.twelvePlan.entity.MesEqualCatalogDO;
import com.anolesoft.twelvePlan.mapper.MesEqualCatalogMapper;
import com.anolesoft.twelvePlan.mapper.MesGyInfoMapper;
import com.anolesoft.twelvePlan.service.MesEqualCatalogService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyq
 * @date 2022/3/21
 */
@Service
@Slf4j
public class MesEqualCatalogServiceImpl implements MesEqualCatalogService {
    @Autowired
    private MesEqualCatalogMapper mesEqualCatalogMapper;
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public PageData<List<MesEqualCatalogDTO>> queryEqualCatalog(MesEqualCatalogDTO mesEqualCatalogDTO, Integer pageNo, Integer pageSize) {
        Page<MesEqualCatalogDO> page = new Page<>(pageNo, pageSize);
        List<MesEqualCatalogDO> mesEqualCatalogDOList = mesEqualCatalogMapper.queryEqualCatalog(MesEqualCatalogDO.builder()
                .jx(CommonUtils.stringNull(mesEqualCatalogDTO.getJx()))
                .partid(CommonUtils.stringNull(mesEqualCatalogDTO.getPartid()))
                .flag(CommonUtils.stringNull(mesEqualCatalogDTO.getFlag()))
                .factoryId(CommonUtils.stringNull(mesEqualCatalogDTO.getFactoryId()))
                .build(), page);
        List<MesEqualCatalogDTO> mesEqualCatalogDTOList = mesEqualCatalogDOList.stream()
                .map(t -> BeanUtil.copyProperties(t, MesEqualCatalogDTO.class))
                .collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), mesEqualCatalogDTOList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEqualCatalog(MesEqualCatalogDTO mesEqualCatalogDTO) {
        int repeat = mesEqualCatalogMapper.repeatEqualCatalog(MesEqualCatalogDO.builder()
                .jx(CommonUtils.stringNull(mesEqualCatalogDTO.getJx()))
                .partid(CommonUtils.stringNull(mesEqualCatalogDTO.getPartid()))
                .build());
        int count = 0;
        if (repeat == 0) {
            count = mesEqualCatalogMapper.insertEqualCatalog(MesEqualCatalogDO.builder()
                    .jx(CommonUtils.stringNull(mesEqualCatalogDTO.getJx()))
                    .partid(CommonUtils.stringNull(mesEqualCatalogDTO.getPartid()))
                    .flag(CommonUtils.stringNull(mesEqualCatalogDTO.getFlag()))
                    .createuid(CommonUtils.stringNull(mesEqualCatalogDTO.getCreateuid()))
                    .factoryId(CommonUtils.stringNull(mesEqualCatalogDTO.getFactoryId()))
                    .build());
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateEqualCatalog(MesEqualCatalogDTO mesEqualCatalogDTO) {
        int repeat = mesEqualCatalogMapper.repeatEqualCatalog(MesEqualCatalogDO.builder()
                .jx(CommonUtils.stringNull(mesEqualCatalogDTO.getJx()))
                .partid(CommonUtils.stringNull(mesEqualCatalogDTO.getPartid()))
                .objuid(CommonUtils.stringNull(mesEqualCatalogDTO.getObjuid()))
                .build());
        int count = 0;
        if (repeat == 0) {
            count = mesEqualCatalogMapper.updateEqualCatalog(MesEqualCatalogDO.builder()
                    .objuid(CommonUtils.stringNull(mesEqualCatalogDTO.getObjuid()))
                    .jx(CommonUtils.stringNull(mesEqualCatalogDTO.getJx()))
                    .partid(CommonUtils.stringNull(mesEqualCatalogDTO.getPartid()))
                    .flag(CommonUtils.stringNull(mesEqualCatalogDTO.getFlag()))
                    .modifyuid(CommonUtils.stringNull(mesEqualCatalogDTO.getModifyuid()))
                    .build());
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteEqualCatalog(MesEqualCatalogDTO mesEqualCatalogDTO) {
        int count = mesEqualCatalogMapper.deleteEqualCatalog(MesEqualCatalogDO.builder()
                .objuid(CommonUtils.stringNull(mesEqualCatalogDTO.getObjuid()))
                .deleteuid(CommonUtils.stringNull(mesEqualCatalogDTO.getDeleteuid()))
                .build());
        return count;
    }

    @Override
    public int exportEqualCatalog(MesEqualCatalogDTO mesEqualCatalogDTO) {
        int count = mesEqualCatalogMapper.exportEqualCatalog(MesEqualCatalogDO.builder()
                .jx(CommonUtils.stringNull(mesEqualCatalogDTO.getJx()))
                .partid(CommonUtils.stringNull(mesEqualCatalogDTO.getPartid()))
                .flag(CommonUtils.stringNull(mesEqualCatalogDTO.getFlag()))
                .factoryId(CommonUtils.stringNull(mesEqualCatalogDTO.getFactoryId()))
                .build());
        return count;
    }

    @Override
    public void uploadEqualCatalog(List<MesEqualCatalogDO> list) {
        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
        MesEqualCatalogMapper mesEqualCatalogMapper = sqlSession.getMapper(MesEqualCatalogMapper.class);
        list.forEach(a -> {
            mesEqualCatalogMapper.insert(a);
        });
        sqlSession.commit();
    }
}
